require "stockpriceprovider/stockpriceprovider.rb"

class IndexController < ApplicationController
  def index
    list
    render :action => 'list'
  end

  # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
  verify :method => :post, :only => [ :destroy, :create, :update ],
         :redirect_to => { :action => :list }

  def list
    @operations = Operation.list
    @history = Operation.historyList
  end

  def buy
    @operation = Operation.new
    @operation.account_id=2 #default
  end

  def sell
    @operation = Operation.find(params[:id])
    @operation.timestamp = DateTime.now
  end

  def makebuy
    @operation = Operation.newforbuy(params[:operation])
    if Operation.buy(@operation)
      redirect_to :action => 'list'
    else
      render :action => 'buy'
    end
  end

  def makesell
    bought = Operation.find(params[:id], :include => [:sell])
    @operation = Operation.newforsell(bought, params[:operation])

    if (not @operation.errors.empty? or not Operation.sell(bought, @operation))
      render :action => 'sell'
    else
      redirect_to :action => 'list'
    end
  end

  def calc
    @operation = Operation.find(params[:id])

    if (params[:sell].nil?)
      @sell = @operation.simulateSelling({})
      @sell.price = @sell.zeroPrice.round_off(2)
    else
      @sell = @operation.simulateSelling(params[:sell])
    end

    render(:layout => false)
  end

  def sandboxbuy
    @operation = Operation.newforbuy(params[:operation])
    session[:sandbox_operation] = @operation

    sell = @operation.deep_clone
    sell.price = sell.zeroPrice.round_off(2)
    @sell = sell

    logger.info("sell : " + @sell.inspect)
    render :action => "index/sandbox", :layout => false
  end
  def sandboxsell
    @operation = session[:sandbox_operation]
    @sell = Operation.newforsell(@operation, params[:sell])
    render :action => "index/sandbox", :layout => false
  end

  def walletTotal
    operations = Operation.list
    inout = Operation.new
    total = Operation.new
    for operation in operations
      sell = operation.simulateSelling({})
      inout.addAmounts(operation.simulateIncomeOutcome(sell))
      total.addAmounts(operation.simulateTotal(sell))
    end

    @total = total
    @inout = inout
    render :layout => false
  end

  def calculateValue(operation)
    price = StockPriceProviderFactory.getStockPriceProvider.priceFor(operation.shares.name).to_f
    operation.currentValue(price)
  end

  def show
    @operation = Operation.find(params[:id])
  end

  def edit
    @operation = Operation.find(params[:id])
  end

  def update
    @operation = Operation.find(params[:id])
    if @operation.update_attributes(params[:operation])
      flash[:notice] = 'Operation was successfully updated.'
      redirect_to :action => 'list'
    else
     render :action => 'edit'
    end
  end

  def delete
    Operation.find(params[:id]).destroy
    redirect_to :action => 'list'
  end

  def currentPrice
    @share = params[:share]
    @price = StockPriceProviderFactory.getStockPriceProvider.priceFor(@share)
    render :partial => "currentPrice"
  end

  def simulateSelling
    operation = Operation.find(params[:id])
    @est = operation.simulateSelling({})
    render :layout => false
  end

  def potentialInOut
    operation = Operation.find(params[:id])
    est = operation.simulateSelling({})
    @inout = operation.simulateIncomeOutcome(est)
    render :layout => false
  end

  def simulateTotal
    operation = Operation.find(params[:id])
    est = operation.simulateSelling({})
    @estTotal = operation.simulateTotal(est)
    render :layout => false
  end

end
